package com.ruoyi.recycle.utils;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


public class OssUpload {

    private static String endpoint = "oss-cn-guangzhou.aliyuncs.com";
    private static String accessKeyId = "LTAI5tJLFZKAtCnfqRWGSoz7";
    private static String accessKeySecret = "H7Z1BoOfIrkWFpjBdTW1yMW3BqjkC2";
    private static String bucketName = "blue-star-human";
    private static String callUrl = "https://blue-star-human.oss-cn-guangzhou.aliyuncs.com/";

    public static String main(byte[] image, String suffix) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(image);
            String keySuffixWithSlash = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String fileMainName = UUID.randomUUID().toString().replace("-", "");
            String objectName = keySuffixWithSlash + "/" + fileMainName + "" + suffix;

            PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream);
            ResponseMessage responseMessage = putObjectResult.getResponse();
            // 根据响应状态码判断请求是否成功
            if (responseMessage == null) {
                String ossFileAccessPath = callUrl + objectName;
                return ossFileAccessPath;
            } else {
                // 获取响应状态码
                int statusCode = responseMessage.getStatusCode();
                String errorMessage = responseMessage.getErrorResponseAsString();
                // 当前方法返回失败
                System.out.println("当前响应状态码=" + statusCode + " 错误消息=" + errorMessage);
            }
        } catch (OSSException oe) {
            oe.getMessage();
        } finally {
            // ossClient.shutdown();
        }
        return null;
    }

    public static String uploadFile(MultipartFile multipartFile, String suffix)
            throws OSSException, ClientException {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            String keySuffixWithSlash = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String fileMainName = UUID.randomUUID().toString().replace("-", "");
            String objectName = keySuffixWithSlash + "/" + fileMainName + "" + suffix;
            if (multipartFile.getSize() != 0 && !"".equals(multipartFile.getName())) {
                // getInputStream()返回一个InputStream以从中读取文件的内容。通过此方法就可以获取到流
                InputStream multipartFileInputStream = multipartFile.getInputStream();
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, multipartFileInputStream);
                PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
                ResponseMessage responseMessage = putObjectResult.getResponse();
                if (responseMessage == null) {
                    String ossFileAccessPath = callUrl + objectName;
                    return ossFileAccessPath;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            ossClient.shutdown();
        }
        return null;
    }

}
